package vista;

import java.awt.Color;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.Stack;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JButton;
import javax.swing.UIManager;
import controlador.Controller;
import modelo.Complex;

/**
 *
 * @author DeveloperDreamTeam
 */
public class GUI extends javax.swing.JFrame {

    private int tipoDeDatos = 3;
    private boolean punto = false;
    private int tipoDeOperacion;
    private Stack<Object> argumentos = new Stack<Object>();
    private boolean borrar = false;
    private boolean bloquear = false;
    private boolean campo = true;
    private boolean imaginario = true;

    /** Creates new form GUI */
    public GUI() {
        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (Exception ex) {
            Logger.getLogger(GUI.class.getName()).log(Level.SEVERE, null, ex);
        }

        initComponents();
        argumento.addKeyListener(new KeyAdapter() {

            @Override
            public void keyTyped(KeyEvent e) {
                char caracter = e.getKeyChar();

                // Adicionar Digito
                if ((caracter >= '0') && (caracter <= '9')) {
                    if (borrar) {
                        argumento.setText("");
                        borrar = false;
                    }
                    return;
                }
                // Adicionar Punto
                if (caracter == '.') {
                    if (punto) {
                        e.consume();
                    } else {
                        punto = true;
                    }
                    return;
                }

                // Operaciones
                if (caracter == '+') {
                    botonSumaActionPerformed(null);
                }

                if (caracter == '-') {
                    botonRestaActionPerformed(null);
                }

                if (caracter == '*') {
                    botonMultiplicacionActionPerformed(null);
                }

                if (caracter == '/') {
                    botonDivisionActionPerformed(null);
                }

                if (caracter == KeyEvent.VK_ENTER) {
                    botonIgualActionPerformed(null);
                }

                if (caracter == KeyEvent.VK_ESCAPE) {
                    botonBorrarTodoActionPerformed(null);
                }
                e.consume();
            }
        });

        argumento1.addKeyListener(new KeyAdapter() {

            @Override
            public void keyTyped(KeyEvent e) {
                char caracter = e.getKeyChar();

                // Adicionar Digito
                if ((caracter >= '0') && (caracter <= '9')) {
                    if (borrar) {
                        argumento1.setText("");
                        borrar = false;
                    }
                    return;
                }
                // Adicionar Punto
                if (caracter == '.') {
                    if (punto) {
                        e.consume();
                    } else {
                        punto = true;
                    }
                    return;
                }

                // Operaciones
                if (caracter == '+') {
                    botonSumaActionPerformed(null);
                }

                if (caracter == '-') {
                    botonRestaActionPerformed(null);
                }

                if (caracter == '*') {
                    botonMultiplicacionActionPerformed(null);
                }

                if (caracter == '/') {
                    botonDivisionActionPerformed(null);
                }

                if (caracter == KeyEvent.VK_ENTER) {
                    botonIgualActionPerformed(null);
                }

                if (caracter == KeyEvent.VK_ESCAPE) {
                    botonBorrarTodoActionPerformed(null);
                }
                e.consume();
            }
        });
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        calculadora = new javax.swing.JPanel();
        boton0 = new javax.swing.JButton();
        boton1 = new javax.swing.JButton();
        boton2 = new javax.swing.JButton();
        boton3 = new javax.swing.JButton();
        boton4 = new javax.swing.JButton();
        boton5 = new javax.swing.JButton();
        boton6 = new javax.swing.JButton();
        boton7 = new javax.swing.JButton();
        boton8 = new javax.swing.JButton();
        boton9 = new javax.swing.JButton();
        botonPunto = new javax.swing.JButton();
        botonSuma = new javax.swing.JButton();
        botonResta = new javax.swing.JButton();
        botonMultiplicacion = new javax.swing.JButton();
        botonDivision = new javax.swing.JButton();
        botonIgual = new javax.swing.JButton();
        botonBorrarDigito = new javax.swing.JButton();
        botonBorrarArgumento = new javax.swing.JButton();
        botonBorrarTodo = new javax.swing.JButton();
        botonSigno = new javax.swing.JButton();
        argumento = new javax.swing.JTextField();
        argumento1 = new javax.swing.JTextField();
        parentesisIzq = new javax.swing.JLabel();
        parentesisDer = new javax.swing.JLabel();
        signoMas = new javax.swing.JLabel();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu3 = new javax.swing.JMenu();
        botonReal = new javax.swing.JRadioButtonMenuItem();
        botonImaginario = new javax.swing.JRadioButtonMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Calc");
        setResizable(false);

        boton0.setFont(new java.awt.Font("Arial", 1, 14));
        boton0.setForeground(java.awt.Color.blue);
        boton0.setText("0");
        boton0.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        boton0.setMargin(new java.awt.Insets(2, 2, 2, 2));
        boton0.setName("0"); // NOI18N
        boton0.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                agregarDigito(evt);
            }
        });

        boton1.setFont(new java.awt.Font("Arial", 1, 14));
        boton1.setForeground(java.awt.Color.blue);
        boton1.setText("1");
        boton1.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        boton1.setMargin(new java.awt.Insets(2, 2, 2, 2));
        boton1.setName("1"); // NOI18N
        boton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                agregarDigito(evt);
            }
        });

        boton2.setFont(new java.awt.Font("Arial", 1, 14));
        boton2.setForeground(java.awt.Color.blue);
        boton2.setText("2");
        boton2.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        boton2.setMargin(new java.awt.Insets(2, 2, 2, 2));
        boton2.setName("2"); // NOI18N
        boton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                agregarDigito(evt);
            }
        });

        boton3.setFont(new java.awt.Font("Arial", 1, 14));
        boton3.setForeground(java.awt.Color.blue);
        boton3.setText("3");
        boton3.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        boton3.setMargin(new java.awt.Insets(2, 2, 2, 2));
        boton3.setName("3"); // NOI18N
        boton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                agregarDigito(evt);
            }
        });

        boton4.setFont(new java.awt.Font("Arial", 1, 14));
        boton4.setForeground(java.awt.Color.blue);
        boton4.setText("4");
        boton4.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        boton4.setMargin(new java.awt.Insets(2, 2, 2, 2));
        boton4.setName("4"); // NOI18N
        boton4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                agregarDigito(evt);
            }
        });

        boton5.setFont(new java.awt.Font("Arial", 1, 14));
        boton5.setForeground(java.awt.Color.blue);
        boton5.setText("5");
        boton5.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        boton5.setMargin(new java.awt.Insets(2, 2, 2, 2));
        boton5.setName("5"); // NOI18N
        boton5.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                agregarDigito(evt);
            }
        });

        boton6.setFont(new java.awt.Font("Arial", 1, 14));
        boton6.setForeground(java.awt.Color.blue);
        boton6.setText("6");
        boton6.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        boton6.setMargin(new java.awt.Insets(2, 2, 2, 2));
        boton6.setName("6"); // NOI18N
        boton6.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                agregarDigito(evt);
            }
        });

        boton7.setFont(new java.awt.Font("Arial", 1, 14));
        boton7.setForeground(java.awt.Color.blue);
        boton7.setText("7");
        boton7.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        boton7.setMargin(new java.awt.Insets(2, 2, 2, 2));
        boton7.setName("7"); // NOI18N
        boton7.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                agregarDigito(evt);
            }
        });

        boton8.setFont(new java.awt.Font("Arial", 1, 14));
        boton8.setForeground(java.awt.Color.blue);
        boton8.setText("8");
        boton8.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        boton8.setMargin(new java.awt.Insets(2, 2, 2, 2));
        boton8.setName("8"); // NOI18N
        boton8.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                agregarDigito(evt);
            }
        });

        boton9.setFont(new java.awt.Font("Arial", 1, 14));
        boton9.setForeground(java.awt.Color.blue);
        boton9.setText("9");
        boton9.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        boton9.setMargin(new java.awt.Insets(2, 2, 2, 2));
        boton9.setName("9"); // NOI18N
        boton9.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                agregarDigito(evt);
            }
        });

        botonPunto.setFont(new java.awt.Font("Arial", 1, 14));
        botonPunto.setForeground(java.awt.Color.blue);
        botonPunto.setText(".");
        botonPunto.setToolTipText("Punto Decimal");
        botonPunto.setMargin(new java.awt.Insets(2, 2, 2, 2));
        botonPunto.setName("."); // NOI18N
        botonPunto.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                agregarDigito(evt);
            }
        });

        botonSuma.setFont(new java.awt.Font("Arial", 1, 14));
        botonSuma.setForeground(java.awt.Color.red);
        botonSuma.setText("+");
        botonSuma.setToolTipText("Suma");
        botonSuma.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        botonSuma.setMargin(new java.awt.Insets(2, 2, 2, 2));
        botonSuma.setName("1"); // NOI18N
        botonSuma.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botonSumaActionPerformed(evt);
            }
        });

        botonResta.setFont(new java.awt.Font("Arial", 1, 14));
        botonResta.setForeground(java.awt.Color.red);
        botonResta.setText("-");
        botonResta.setToolTipText("Resta");
        botonResta.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        botonResta.setMargin(new java.awt.Insets(2, 2, 2, 2));
        botonResta.setName("2"); // NOI18N
        botonResta.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botonRestaActionPerformed(evt);
            }
        });

        botonMultiplicacion.setFont(new java.awt.Font("Arial", 1, 14));
        botonMultiplicacion.setForeground(java.awt.Color.red);
        botonMultiplicacion.setText("*");
        botonMultiplicacion.setToolTipText("Multiplicacion");
        botonMultiplicacion.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        botonMultiplicacion.setMargin(new java.awt.Insets(2, 2, 2, 2));
        botonMultiplicacion.setName("3"); // NOI18N
        botonMultiplicacion.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botonMultiplicacionActionPerformed(evt);
            }
        });

        botonDivision.setFont(new java.awt.Font("Arial", 1, 14));
        botonDivision.setForeground(java.awt.Color.red);
        botonDivision.setText("/");
        botonDivision.setToolTipText("Division");
        botonDivision.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        botonDivision.setMargin(new java.awt.Insets(2, 2, 2, 2));
        botonDivision.setName("4"); // NOI18N
        botonDivision.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botonDivisionActionPerformed(evt);
            }
        });

        botonIgual.setFont(new java.awt.Font("Arial", 1, 14));
        botonIgual.setForeground(java.awt.Color.red);
        botonIgual.setText("=");
        botonIgual.setToolTipText("Resultado");
        botonIgual.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        botonIgual.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botonIgualActionPerformed(evt);
            }
        });

        botonBorrarDigito.setFont(new java.awt.Font("Arial", 0, 14));
        botonBorrarDigito.setForeground(java.awt.Color.magenta);
        botonBorrarDigito.setText("←");
        botonBorrarDigito.setToolTipText("Retroceso");
        botonBorrarDigito.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        botonBorrarDigito.setMargin(new java.awt.Insets(2, 2, 2, 2));
        botonBorrarDigito.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                borrarDigito(evt);
            }
        });

        botonBorrarArgumento.setFont(new java.awt.Font("Arial", 0, 14));
        botonBorrarArgumento.setForeground(java.awt.Color.magenta);
        botonBorrarArgumento.setText("CE");
        botonBorrarArgumento.setToolTipText("Borrar");
        botonBorrarArgumento.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        botonBorrarArgumento.setMargin(new java.awt.Insets(2, 2, 2, 2));
        botonBorrarArgumento.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botonBorrarArgumentoActionPerformed(evt);
            }
        });

        botonBorrarTodo.setFont(new java.awt.Font("Arial", 0, 14));
        botonBorrarTodo.setForeground(java.awt.Color.magenta);
        botonBorrarTodo.setText("C");
        botonBorrarTodo.setToolTipText("Borra Todo");
        botonBorrarTodo.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        botonBorrarTodo.setMaximumSize(new java.awt.Dimension(29, 27));
        botonBorrarTodo.setMinimumSize(new java.awt.Dimension(29, 27));
        botonBorrarTodo.setPreferredSize(new java.awt.Dimension(29, 27));
        botonBorrarTodo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botonBorrarTodoActionPerformed(evt);
            }
        });

        botonSigno.setFont(new java.awt.Font("Arial", 1, 14));
        botonSigno.setForeground(java.awt.Color.blue);
        botonSigno.setText("+/-");
        botonSigno.setToolTipText("Cambiar Signo");
        botonSigno.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        botonSigno.setMargin(new java.awt.Insets(2, 2, 2, 2));
        botonSigno.setName("+"); // NOI18N
        botonSigno.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botonSignoActionPerformed(evt);
            }
        });

        argumento.setHorizontalAlignment(javax.swing.JTextField.RIGHT);
        argumento.setToolTipText("Parte Real");
        argumento.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                argumentoMouseClicked(evt);
            }
        });

        argumento1.setHorizontalAlignment(javax.swing.JTextField.RIGHT);
        argumento1.setToolTipText("Parte Imaginaria");
        argumento1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                argumento1MouseClicked(evt);
            }
        });

        parentesisIzq.setText("(");

        parentesisDer.setText(")");

        signoMas.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        signoMas.setText("+ i");

        javax.swing.GroupLayout calculadoraLayout = new javax.swing.GroupLayout(calculadora);
        calculadora.setLayout(calculadoraLayout);
        calculadoraLayout.setHorizontalGroup(
            calculadoraLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(calculadoraLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(calculadoraLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(calculadoraLayout.createSequentialGroup()
                        .addGroup(calculadoraLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(calculadoraLayout.createSequentialGroup()
                                .addGroup(calculadoraLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(botonBorrarArgumento, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 43, Short.MAX_VALUE)
                                    .addComponent(boton1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 43, Short.MAX_VALUE))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(calculadoraLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(botonBorrarTodo, javax.swing.GroupLayout.DEFAULT_SIZE, 43, Short.MAX_VALUE)
                                    .addComponent(boton2, javax.swing.GroupLayout.DEFAULT_SIZE, 43, Short.MAX_VALUE)))
                            .addGroup(calculadoraLayout.createSequentialGroup()
                                .addComponent(boton0, javax.swing.GroupLayout.DEFAULT_SIZE, 43, Short.MAX_VALUE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(botonSigno)))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(calculadoraLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addGroup(calculadoraLayout.createSequentialGroup()
                                .addGroup(calculadoraLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(botonPunto, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(boton3))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(calculadoraLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(botonSuma, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(botonResta)))
                            .addComponent(botonIgual)))
                    .addGroup(calculadoraLayout.createSequentialGroup()
                        .addComponent(boton4, javax.swing.GroupLayout.DEFAULT_SIZE, 43, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(boton5, javax.swing.GroupLayout.PREFERRED_SIZE, 41, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(boton6)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(botonMultiplicacion))
                    .addGroup(calculadoraLayout.createSequentialGroup()
                        .addGap(3, 3, 3)
                        .addComponent(parentesisIzq)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(argumento, javax.swing.GroupLayout.PREFERRED_SIZE, 52, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(2, 2, 2)
                        .addComponent(signoMas)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(argumento1, javax.swing.GroupLayout.PREFERRED_SIZE, 52, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(parentesisDer)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(botonBorrarDigito, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(calculadoraLayout.createSequentialGroup()
                        .addComponent(boton7, javax.swing.GroupLayout.PREFERRED_SIZE, 41, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(boton8, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(boton9, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(botonDivision, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)))
                .addContainerGap())
        );

        calculadoraLayout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {boton0, boton1, boton2, boton3, boton4, boton5, boton6, boton7, boton8, boton9, botonBorrarArgumento, botonBorrarDigito, botonBorrarTodo, botonDivision, botonMultiplicacion, botonPunto, botonResta, botonSigno, botonSuma});

        calculadoraLayout.setVerticalGroup(
            calculadoraLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(calculadoraLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(calculadoraLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(parentesisIzq, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(argumento, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(signoMas)
                    .addComponent(argumento1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(parentesisDer, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(botonBorrarDigito))
                .addGap(18, 18, 18)
                .addGroup(calculadoraLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(boton7, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(boton8)
                    .addComponent(boton9)
                    .addComponent(botonDivision))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(calculadoraLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(boton4)
                    .addComponent(boton5)
                    .addComponent(boton6)
                    .addComponent(botonMultiplicacion))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(calculadoraLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(boton1)
                    .addComponent(botonResta)
                    .addComponent(boton3)
                    .addComponent(boton2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(calculadoraLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(boton0)
                    .addComponent(botonSigno)
                    .addComponent(botonPunto)
                    .addComponent(botonSuma))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(calculadoraLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(botonBorrarArgumento)
                    .addComponent(botonIgual)
                    .addComponent(botonBorrarTodo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        calculadoraLayout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[] {boton0, boton1, boton2, boton3, boton4, boton5, boton6, boton7, boton8, boton9, botonDivision, botonMultiplicacion, botonPunto, botonResta, botonSigno, botonSuma});

        jMenu3.setText("Tipo");

        botonReal.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_R, java.awt.event.InputEvent.CTRL_MASK));
        botonReal.setText("Real");
        botonReal.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botonRealActionPerformed(evt);
            }
        });
        jMenu3.add(botonReal);

        botonImaginario.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_I, java.awt.event.InputEvent.CTRL_MASK));
        botonImaginario.setSelected(true);
        botonImaginario.setText("Imaginario");
        botonImaginario.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botonImaginarioActionPerformed(evt);
            }
        });
        jMenu3.add(botonImaginario);

        jMenuBar1.add(jMenu3);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(calculadora, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(24, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(calculadora, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-242)/2, (screenSize.height-307)/2, 242, 307);
    }// </editor-fold>//GEN-END:initComponents
    private void agregarDigito(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_agregarDigito
        if (!bloquear) {
            JButton boton = (JButton) evt.getSource();
            if (borrar) {
                argumento.setText("");
                argumento1.setText("");
                borrar = false;
            }

            if (!boton.getName().equals(".")) {
                if (campo) {
                    argumento.setText(argumento.getText() + boton.getName());
                } else {
                    argumento1.setText(argumento1.getText() + boton.getName());
                }
            }
            if (boton.getName().equals(".") && !punto) {
                if (campo) {
                    argumento.setText(argumento.getText() + boton.getName());
                } else {
                    argumento1.setText(argumento1.getText() + boton.getName());
                }
                punto = true;
                tipoDeDatos = 2;
            }           
        }
}//GEN-LAST:event_agregarDigito

    private void borrarDigito(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_borrarDigito
        if (!bloquear) {
            String original;
            if (campo) {
                original = argumento.getText();
                if (!original.equals("")) {
                    if (argumento.getText().charAt(argumento.getText().length() - 1) == '.') {
                        punto = false;
                    }
                    argumento.setText(original.substring(0, original.length() - 1));
                }
            } else {
                original = argumento1.getText();
                if (!original.equals("")) {
                    if (argumento1.getText().charAt(argumento1.getText().length() - 1) == '.') {
                        punto = false;
                    }
                    argumento1.setText(original.substring(0, original.length() - 1));
                }
            }
        }
}//GEN-LAST:event_borrarDigito

    private void botonSignoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botonSignoActionPerformed
        if (!bloquear) {
            String original;
            if (campo) {
                if (argumento.getText().contains("-")) {
                    original = argumento.getText();
                    argumento.setText(original.substring(1, original.length()));
                } else {
                    argumento.setText("-" + argumento.getText());
                }
            } else {
                if (argumento1.getText().contains("-")) {
                    original = argumento1.getText();
                    argumento1.setText(original.substring(1, original.length()));
                } else {
                    argumento1.setText("-" + argumento1.getText());
                }
            }
        }
}//GEN-LAST:event_botonSignoActionPerformed

    private void botonRealActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botonRealActionPerformed
        tipoDeDatos = 1;
        botonImaginario.setSelected(false);
        botonReal.setSelected(true);
        argumento1.setVisible(false);
        argumento.setSize(130, (int) argumento.getSize().getHeight());
        parentesisIzq.setForeground(this.getBackground());
        parentesisDer.setForeground(this.getBackground());
        argumento.setText("");
        argumento1.setText("");
        argumentos.removeAllElements();
        campo = true;
        imaginario = false;
}//GEN-LAST:event_botonRealActionPerformed

    private void botonImaginarioActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botonImaginarioActionPerformed
        tipoDeDatos = 3;
        botonReal.setSelected(false);
        botonImaginario.setSelected(true);
        argumento.setSize(50, (int) argumento.getSize().getHeight());
        argumento1.setVisible(true);
        parentesisIzq.setForeground(Color.BLACK);
        parentesisDer.setForeground(Color.BLACK);
        argumento.setText("");
        argumento1.setText("");
        argumentos.removeAllElements();
        imaginario = true;
    }//GEN-LAST:event_botonImaginarioActionPerformed

    private void botonBorrarTodoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botonBorrarTodoActionPerformed
        argumentos.removeAllElements();
        argumento.setText("");
        argumento1.setText("");
        bloquear = false;
    }//GEN-LAST:event_botonBorrarTodoActionPerformed

    private void botonBorrarArgumentoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botonBorrarArgumentoActionPerformed
        if (!bloquear) {
            if (!imaginario) {
                argumento.setText("");
            } else {
                argumento.setText("");
                argumento1.setText("");
            }
            punto = false;
        }
}//GEN-LAST:event_botonBorrarArgumentoActionPerformed

    private void botonDivisionActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botonDivisionActionPerformed
        if (!bloquear) {
            botonIgualActionPerformed(null);
            tipoDeOperacion = 4;
            if (!imaginario) {
                if (!argumento.getText().equals("")) {
                    argumentos.push(argumento.getText());
                }
            } else {
                if (!argumento.getText().equals("") && !argumento1.getText().equals("")) {
                    Complex argument = new Complex(Double.valueOf(argumento.getText()), Double.valueOf(argumento1.getText()));
                    argumentos.push(argument);
                }
            }
            punto = false;
            borrar = true;
        }
    }//GEN-LAST:event_botonDivisionActionPerformed

    private void botonMultiplicacionActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botonMultiplicacionActionPerformed
        if (!bloquear) {
            botonIgualActionPerformed(null);
            tipoDeOperacion = 3;
            if (!imaginario) {
                if (!argumento.getText().equals("") && !argumento.getText().equals("Error:") && !argumento.getText().equals("/ por 0")) {
                    argumentos.push(argumento.getText());
                }
            } else {
                if (!argumento.getText().equals("") && !argumento1.getText().equals("") && !argumento.getText().equals("Error: ") && !argumento.getText().equals("Error: división por cero")) {
                    Complex argument = new Complex(Double.valueOf(argumento.getText()), Double.valueOf(argumento1.getText()));
                    argumentos.push(argument);
                }
            }
            punto = false;
            borrar = true;
        }
    }//GEN-LAST:event_botonMultiplicacionActionPerformed

    private void botonRestaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botonRestaActionPerformed
        if (!bloquear) {
            botonIgualActionPerformed(null);
            tipoDeOperacion = 2;
            if (!imaginario) {
                if (!argumento.getText().equals("")) {
                    argumentos.push(argumento.getText());
                }
            } else {
                if (!argumento.getText().equals("") && !argumento1.getText().equals("")) {
                    Complex argument = new Complex(Double.valueOf(argumento.getText()), Double.valueOf(argumento1.getText()));
                    argumentos.push(argument);
                }
            }
            punto = false;
            borrar = true;
        }
    }//GEN-LAST:event_botonRestaActionPerformed

    private void botonSumaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botonSumaActionPerformed
        if (!bloquear) {
            botonIgualActionPerformed(null);
            tipoDeOperacion = 1;
            if (!imaginario) {
                if (!argumento.getText().equals("")) {
                    argumentos.push(argumento.getText());
                }
            } else {
                if (!argumento.getText().equals("") && !argumento1.getText().equals("")) {
                    Complex argument = new Complex(Double.valueOf(argumento.getText()), Double.valueOf(argumento1.getText()));
                    argumentos.push(argument);
                }
            }
            punto = false;
            borrar = true;
        }
    }//GEN-LAST:event_botonSumaActionPerformed

    private void botonIgualActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botonIgualActionPerformed
        if (!bloquear) {
            if (!imaginario) {
                if (argumentos.size() == 1) {
                    if (argumento.getText().equals("")) {
                        argumentos.push(argumentos.get(0));
                    } else {
                        argumentos.push(argumento.getText());
                    }

                    String arg0 = (String) argumentos.get(0);
                    String arg1 = (String) argumentos.get(1);
                    if (argumento.getText().contains(".") || arg0.contains(".")) {
                        tipoDeDatos = 2;
                    } else {
                        tipoDeDatos = 1;
                    }
                    if (tipoDeOperacion == 4 && argumento.getText().equals("0")) {
                        argumento.setText("Error: división por cero");
                        bloquear = true;
                        argumentos.removeAllElements();
                        borrar = true;
                        return;
                    }
                    if (!arg0.equals(".") && !arg1.equals(".")) {
                        argumento.setText("" + Controller.execute(tipoDeOperacion, tipoDeDatos, argumentos));
                    } else {
                        argumento.setText("");
                    }
                    argumentos.removeAllElements();
                    borrar = true;
                }
            } else {
                if (argumentos.size() == 1) {
                    if(argumento.getText().isEmpty())
                        argumento.setText("0");
                    if(argumento1.getText().isEmpty())
                        argumento1.setText("0");
                    
                    Complex argument = new Complex(Double.valueOf(argumento.getText()), Double.valueOf(argumento1.getText()));
                    argumentos.push(argument);
                    tipoDeDatos = 3;
                    if (tipoDeOperacion == 4 && (argumento.getText().equals("0") && argumento1.getText().equals("0"))) {
                        argumento.setText("Error:");
                        argumento1.setText("/ por 0");
                        bloquear = true;
                        argumentos.removeAllElements();
                        borrar = true;
                        return;
                    }
                    if (!argumento.getText().equals(".") && !argumento1.getText().equals(".")) {
                        Complex resultado = (Complex) Controller.execute(tipoDeOperacion, tipoDeDatos, argumentos);
                        argumento.setText(String.valueOf(resultado.getReal()));
                        argumento1.setText(String.valueOf(resultado.getImag()));
                    } else {
                        argumento.setText("");
                        argumento1.setText("");
                    }
                    argumentos.removeAllElements();
                    borrar = true;
                }
            }
        }
    }//GEN-LAST:event_botonIgualActionPerformed

    private void argumentoMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_argumentoMouseClicked
        campo = true;
    }//GEN-LAST:event_argumentoMouseClicked

    private void argumento1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_argumento1MouseClicked
        campo = false;
    }//GEN-LAST:event_argumento1MouseClicked

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new GUI().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField argumento;
    private javax.swing.JTextField argumento1;
    private javax.swing.JButton boton0;
    private javax.swing.JButton boton1;
    private javax.swing.JButton boton2;
    private javax.swing.JButton boton3;
    private javax.swing.JButton boton4;
    private javax.swing.JButton boton5;
    private javax.swing.JButton boton6;
    private javax.swing.JButton boton7;
    private javax.swing.JButton boton8;
    private javax.swing.JButton boton9;
    private javax.swing.JButton botonBorrarArgumento;
    private javax.swing.JButton botonBorrarDigito;
    private javax.swing.JButton botonBorrarTodo;
    private javax.swing.JButton botonDivision;
    private javax.swing.JButton botonIgual;
    private javax.swing.JRadioButtonMenuItem botonImaginario;
    private javax.swing.JButton botonMultiplicacion;
    private javax.swing.JButton botonPunto;
    private javax.swing.JRadioButtonMenuItem botonReal;
    private javax.swing.JButton botonResta;
    private javax.swing.JButton botonSigno;
    private javax.swing.JButton botonSuma;
    private javax.swing.JPanel calculadora;
    private javax.swing.JMenu jMenu3;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JLabel parentesisDer;
    private javax.swing.JLabel parentesisIzq;
    private javax.swing.JLabel signoMas;
    // End of variables declaration//GEN-END:variables
}
